const mysql = require('mysql2/promise');

/**
 * 创建character_training表
 */
async function createCharacterTrainingTable() {
    console.log('🔧 创建character_training表...');

    const connection = await mysql.createConnection({
        host: '43.143.253.188',
        port: 3306,
        user: 'nextjs',
        password: 'yEAiRE2jcxTyRBy5',
        database: 'nextjs',
        charset: 'utf8mb4'
    });

    try {
        // 设置MySQL会话配置
        await connection.execute("SET SESSION sql_mode = ''");
        await connection.execute("SET SESSION time_zone = '+08:00'");
        await connection.execute("SET SESSION FOREIGN_KEY_CHECKS=0");
        console.log('✅ MySQL会话配置设置完成');

        // 创建character_training表
        const createTableSQL = `CREATE TABLE IF NOT EXISTS character_training (
            id int NOT NULL AUTO_INCREMENT,
            characterId int NOT NULL,
            isTraining tinyint NOT NULL DEFAULT 0,
            training_start_time datetime NULL,
            training_end_time datetime NULL,
            training_status tinyint NOT NULL DEFAULT 0,
            training_type int NULL,
            training_duration int NULL,
            created_at datetime NOT NULL COMMENT '创建时间',
            updated_at datetime NULL COMMENT '更新时间',
            PRIMARY KEY (id),
            KEY characterId (characterId)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色训练表'`;

        await connection.execute(createTableSQL);
        console.log('✅ character_training表创建成功');

    } catch (error) {
        if (error.code === 'ER_TABLE_EXISTS_ERROR') {
            console.log('⚠️ character_training表已存在，跳过');
        } else {
            console.error('❌ 创建character_training表失败:', error.message);
        }
    } finally {
        await connection.end();
    }
}

// 执行创建
createCharacterTrainingTable();